import {
    applyDecorators,
    Logger,
    SetMetadata,
    UnsupportedMediaTypeException,
    UseGuards,
    UseInterceptors,
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface'
import { AuthGuard } from '@nestjs/passport'
import { Role } from '@/auth/unum/role'
import { AuthGuardGuard } from '@/auth/auth-guard/auth-guard.guard'

export function upload(fieldName = 'file', options: MulterOptions = {}) {
    return applyDecorators(UseInterceptors(FileInterceptor(fieldName, options)))
}

export function Auth(...Role: Role[]) {
    // return applyDecorators(SetMetadata('role', Role), UseGuards(AuthGuard('jwt'), AuthGuardGuard))
    return applyDecorators(UseGuards(AuthGuard('jwt')))
}

export function fileMimetypeFilter(...mimes: string[]) {
    return (req: any, file: Express.Multer.File, callback: (error: Error | null, acceptFile: boolean) => void) => {
        new Logger().log(file)
        if (mimes.some((mime) => file.mimetype.includes(mime))) {
            callback(null, true)
        } else {
            callback(new UnsupportedMediaTypeException('文件上传类型错误'), false)
        }
    }
}
 